var fs = require('fs');
var path = require('path');

// 创建一个可读流
var readerStream = fs.createReadStream(path.resolve(__dirname, 'xuanchuanpian.mp4'));

readerStream.on('data', (chunk) => {
  console.log(`Received ${chunk.length} bytes of data.`);
});

readerStream.on('end', (data) => {
  console.log('Data reading is finished.', data?.length);
  readerStream.close();
});

readerStream.on('close', () => {
  console.log('Stream is closed.');
});
// 创建一个可写流
var writerStream = fs.createWriteStream(path.resolve(__dirname, 'xuanchuanpian-copy.mp4'));

// 管道读写操作
// 读取 input.txt 文件内容，并将内容写入到 output.txt 文件中
readerStream.pipe(writerStream);

console.log('程序执行完毕');
writerStream.on('finish', function () {
  console.log('写入完成。');
});

writerStream.on('error', function (err) {
  console.log(err.stack);
});
